//
//  FKHideNavVC.m
//  TimesCloud
//
//  Created by frank on 2018/11/7.
//  Copyright © 2018年 mfwlzlg. All rights reserved.
//

#import "FKHideNavVC.h"

@interface FKHideNavVC ()<UINavigationControllerDelegate>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *navBar;
@property (nonatomic, strong) UIButton *leftItem;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *rightItem;

@end

@implementation FKHideNavVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self setupNav];
    
    if (self.isNeedRemoveBeforeVc) {
        [self fk_removeBeforeVC];
    }
}

- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fkOffsetTop + fkStatusAndNaBarH)];
        // 设置这个没有用，只能显示在最上层，但是无法响应事件
        _topView.layer.zPosition = MAXFLOAT;
    }
    return _topView;
}

- (UIView *)navBar
{
    if (!_navBar) {
        CGFloat navY = [UIApplication sharedApplication].statusBarFrame.size.height;
        _navBar = [[UIView alloc] initWithFrame:CGRectMake(0, navY, fkScreenW, fkNavBarH)];
    }
    return _navBar;
}

- (UIButton *)leftItem
{
    if (!_leftItem) {
        _leftItem = [UIButton fk_btnWithTarget:self action:@selector(leftItemClicked) img:nil];
        _leftItem.frame = CGRectMake(20, 0, fkNavBarH, fkNavBarH);
        _leftItem.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }
    return _leftItem;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(18) textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter];
    }
    return _titleLabel;
}

- (UIButton *)rightItem
{
    if (!_rightItem) {
        _rightItem = [UIButton fk_btnWithTarget:self action:@selector(rightItemClicked) img:nil];
        _rightItem.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        // 根据研究，左边的间距为20
        _rightItem.titleLabel.font = fk_adjustFont(16);
        _rightItem.frame = CGRectMake(fkScreenW - 20 - fkNavBarH, 0, fkNavBarH, fkNavBarH);
    }
    return _rightItem;
}

- (void)showLeftItem
{
    [self addSubviews];
    [self.navBar addSubview:self.leftItem];
    [self.leftItem setImage:[UIImage imageNamed:@"icon_white_back"] forState:UIControlStateNormal];
}

- (void)addSubviews
{
    if (self.isHideNavBar) {
        if (self.topView) {
            [self.topView removeFromSuperview];
        }
        return;
    }
    [self.view addSubview:self.topView];
    [self.topView addSubview:self.navBar];
}

- (void)leftItemClicked
{
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:true];
    }
}

- (void)rightItemClicked
{
    
}

- (void)setTitle:(NSString *)title
{
    [super setTitle:title];

    [self addSubviews];
    [self.navBar addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.offset(0);
    }];
    self.titleLabel.text = title;
}

- (void)setTitleColor:(UIColor *)titleColor
{
    _titleColor = titleColor;
    
    [self addSubviews];
    [self.navBar addSubview:self.titleLabel];
    self.titleLabel.textColor = titleColor;
}

- (void)setLeftImgName:(NSString *)leftImgName
{
    _leftImgName = leftImgName;
    
    [self addSubviews];
    [self.navBar addSubview:self.leftItem];
    [self.leftItem setImage:[UIImage imageNamed:leftImgName] forState:UIControlStateNormal];
}

- (void)setRightImgName:(NSString *)rightImgName
{
    _rightImgName = rightImgName;
    
    [self addSubviews];
    [self.navBar addSubview:self.rightItem];
    [self.rightItem setImage:[UIImage imageNamed:rightImgName] forState:UIControlStateNormal];
}

- (void)setRightTitle:(NSString *)rightTitle
{
    _rightTitle = rightTitle;
    
    [self addSubviews];
    [self.navBar addSubview:self.rightItem];
    [self.rightItem setTitle:rightTitle forState:UIControlStateNormal];
    self.rightItem.fk_width = [self.rightItem.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : self.rightItem.titleLabel.font}].width + 2;
}

#warning 直接在FKNavVC中的UINavigationControllerDelegate方法中处理了，就不用在这里处理了
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//
//    // 设置导航控制器的代理为self
//    self.navigationController.delegate = self;
//}
//
//#pragma mark - UINavigationControllerDelegate
//// 将要显示控制器
//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    [self.navigationController setNavigationBarHidden:[viewController isKindOfClass:[self class]] animated:YES];
//}
//
//- (void)dealloc {
//    self.navigationController.delegate = nil;
//}

@end
